package com.tool_stdy.ConcurrencyModel.WorkMastser;

import java.util.Map;
import java.util.Queue;

/**
 * Created by yan_li on 2016/11/11.
 */
public class Worker implements Runnable {

    private Queue<Task> workQueue;

    private Map<String, Task> resultMap;

    public void setWorkQueue(Queue<Task> workQueue) {
        this.workQueue = workQueue;
    }

    public void setResultMap(Map<String, Task> resultMap) {
        this.resultMap = resultMap;
    }

    public Task handle(Task input) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        input.setPrice(10);
        // System.out.println(Thread.currentThread().getName()+" run..");
        return input;
    }

    @Override
    public void run() {
        while (true) {
            Task input = workQueue.poll();
            if (null == input) {
                break;
            }
            //处理子任务
            Task re = handle(input);
            resultMap.put(Integer.toString(input.hashCode()), re);
        }
    }
}
